package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;


    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Override
    public ApartmentItemVo queryApartmentByApartId(Long apartmentId) {
        //根据公寓id查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.getByApartmentId(apartmentId);
        if(apartmentInfo==null){
            return null;
        }
        //根据公寓id查询公寓标签列表
        List<LabelInfo> labelInfoList=labelInfoMapper.queryLabelByApartId(apartmentId);
        //根据公寓id查询图片列表
        List<GraphInfo> graphInfoList=graphInfoMapper.queryGraphByApartId(ItemType.APARTMENT,apartmentId);
        //根据公寓id查询rent对象
        BigDecimal minRent=roomInfoMapper.queryMinRentByApartId(apartmentId);
        //创建vo来接收以上列表信息
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        //将原有的信息复制到新的vo中
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);

        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);
        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo queryApartmentById(Long id) {
        //查询公寓
        ApartmentInfo apartmentInfo= apartmentInfoMapper.selectByApartmentId(id);
        if(apartmentInfo==null){
            return null;
        }
        //查询公寓图片
        List<GraphInfo> graphInfoList=graphInfoMapper.queryGraphByApartId(ItemType.APARTMENT,id);
        //查询你公寓标签
        List<LabelInfo> labelInfoList=labelInfoMapper.queryLabelByApartId(id);
        //查询配套
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.queryFacilityByApartId(id);
        //查询租金最小值
        BigDecimal minRent=roomInfoMapper.queryMinRentByApartId(id);

        ReleaseStatus isRelease = apartmentInfo.getIsRelease();

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        //设置图片
        apartmentDetailVo.setGraphVoList(graphInfoList);
        //设置标签
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //设置配套
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        //设置最小值
        apartmentDetailVo.setMinRent(minRent);
        //公寓状态
        apartmentDetailVo.setIsRelease(isRelease);
        return apartmentDetailVo;
    }
}




